package com.tj.jinzao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseListAdapter;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.LiveBean;
import com.tj.jinzao.widgets.RoundedImageView;

import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 直播 Adapter
 * Created by wjy on 2019/1/10.
 */
public class LiveListItemAdapter extends BaseListAdapter<LiveBean, LiveListItemAdapter.ViewHolder> {


    public LiveListItemAdapter(Context context, CopyOnWriteArrayList data) {
        super(context, data);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_live_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        LiveBean live = mData.get(position);
        if(live!=null){
        if (live.getLiveStatus().equals("true")) {
            holder.status.setText("直播中");
            holder.status.setEnabled(true);
            holder.status.setActivated(true);
            holder.replay.setVisibility(View.GONE);
        }
        else {
            holder.status.setText("未开始");
            holder.status.setEnabled(true);
            holder.status.setActivated(false);
            holder.replay.setVisibility(View.GONE);
        }
        if(live.getLiveTime()!=null&&!live.getLiveTime().equals("")){
            holder.time.setText(live.getLiveTime());
        }else {
            holder.time.setText("");
        }

            RequestOptions options = new RequestOptions();
             options.placeholder((live.getWebUser()!=null&&live.getWebUser().getGender() == 1)
                        ? R.mipmap.default_head_women : R.mipmap.default_head_man);
             if(live.getWebUser()!=null){
                 Glide.with(mContext).load(Constant.IMAGE_PREFIX+live.getWebUser().getImgurl()).apply(options).into(holder.head);
                 holder.name.setText(live.getWebUser().getNickname());

             }else {
                 holder.name.setText("");
                 holder.head.setImageResource(R.mipmap.default_head_women);
             }
             if(live.getPersonCount()==null||live.getPersonCount().equals("0")){
                 holder.tv_views.setText("0人观看");
             }else {
                 holder.tv_views.setText(live.getPersonCount()+"人观看");
             }
            holder.title.setText(live.getTitle());
            Glide.with(mContext).load(Constant.IMAGE_PREFIX+live.getImgurl()).into(holder.bg);

        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RoundedImageView bg;
        TextView title, status, time, replay, name,tv_views;
        RoundedImageView head;

        public ViewHolder(View itemView) {
            super(itemView);
            bg = itemView.findViewById(R.id.iv_bg);
            name = itemView.findViewById(R.id.tv_name);
            title = itemView.findViewById(R.id.tv_title);
            time = itemView.findViewById(R.id.tv_time);
            replay = itemView.findViewById(R.id.tv_replay);
            status = itemView.findViewById(R.id.tv_status);
            head = itemView.findViewById(R.id.iv_head);
            tv_views = itemView.findViewById(R.id.tv_views);
        }
    }


}
